package com.ews.onlineexchange.service;

import java.io.Serializable;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonReader;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.transaction.Transactional;

import com.ews.onlineexchange.model.DeterminedRecord;

@Named
@Stateless
public class DeterminedRecordService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	EntityManager entityManager;
	@Transactional
	public void persisterRecord(DeterminedRecord record) {
		entityManager.persist(record);
	}
	
	private final static String IS_EXIST_SUPPLIER_IN_RECORD = "" 
			+ " SELECT COUNT(record) "
			+ " FROM DeterminedRecord record"
			+ " WHERE record.delFlag = 0 "
			+ " AND record.supplier.id = :supplierId"
			+ " AND ((record.auditted IS NULL OR record.auditted =0 ) AND record.recordStatus = 'Deleted' "
			+ "	OR record.recordStatus  != 'Deleted' ) ";
	/**
	 * @param supplierId
	 * @return
	 * 验证中介是否在待定名单中还有未整改完成的记录
	 */
	public boolean isExistSupplierInRecord(String supplierId) {
		Query query = entityManager.createQuery(IS_EXIST_SUPPLIER_IN_RECORD)
						.setParameter("supplierId", supplierId);
		long count = (long)query.getSingleResult();
		if(count > 0) return true;
		else return false;
	}
			
}
